/****************************************************************************/
/* NAME:           Hal_UsartManagement.c                                    */
/* PURPOSE:        Manage all USART                                         */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  03/07/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "Hal_UsartManagement.h"
#include "Hal_UsartManagement_Config.h"
#include "stdio.h"

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

#define USART_NUMBER  (sizeof(UsartConfigList) / sizeof(UsartConfigList[0]))

/**
  * @brief  Main function of usart management.
  *         This function is execute one time at the start.
  * @param  none
  * @retval None
  */
void vTaskCTRL_UsartManagementInit(void)
{
  USART_InitTypeDef USART_InitStructure;
  uint8_t           i;

  for (i=0; i<USART_NUMBER; i++)
  {
    if (UsartConfigList[i].UsartName == USART_1)
    {
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    }
    if (UsartConfigList[i].UsartName == USART_2)
    {
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    }
    if (UsartConfigList[i].UsartName == USART_3)
    {
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
    }

    USART_InitStructure.USART_BaudRate            = UsartConfigList[i].BaudRate;
    USART_InitStructure.USART_WordLength          = UsartConfigList[i].WordLength;
    USART_InitStructure.USART_StopBits            = UsartConfigList[i].StopBits;
    USART_InitStructure.USART_Parity              = UsartConfigList[i].Parity;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(UsartConfigList[i].UsartName, &USART_InitStructure);
    USART_Cmd(UsartConfigList[i].UsartName, ENABLE);
  }

  #if (defined(STDOUT_USART) || defined(STDIN_USART) || defined(STDERR_USART))
    /* specific use for new lib support printf usart */
    /* turn off buffers, so IO occurs immediately    */
    setvbuf(stdin,  NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
  #endif
}

uint8_t USART_Scanf(uint32_t value)
{
  uint32_t ind = 0;
  uint32_t tmp[2] = {0, 0};

  while (ind < 2)
  {
    // Loop until RXNE = 1
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
    {}
    tmp[ind++] = (USART_ReceiveData(USART1));
    if ((tmp[ind - 1] < 0x30) || (tmp[ind - 1] > 0x39))
    {
      printf("\n\rPlease enter valid number between 0 and 9");
      ind--;
    }
  }
  // Calculate the Corresponding value
  ind = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
  // Checks
  if (ind > value)
  {
    printf("\n\rPlease enter valid number between 0 and %ld", value);
    return 0xFF;
  }
  return ind;
}



